package com.ruoyi.finishedProduct.controller;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.finishedProduct.domain.FinishedProduct;
import com.ruoyi.finishedProduct.service.FinishedProductService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestParam;
import org.springframework.web.bind.annotation.ResponseBody;
import org.springframework.web.bind.annotation.RestController;

import java.util.List;

/**
 * @Author
 * @Version 1.0
 */
@RestController
@RequestMapping("/finishedProduct")
public class FinishedProductController extends BaseController {

    @Autowired
    FinishedProductService finishedProductService;

    /**
     * 保存成品入库报告
     */
    @Log(title = "保存成品入库报告", businessType = BusinessType.INSERT)
    @PostMapping("/addFinishedProduct")
    public AjaxResult addFinishedProduct(@RequestBody FinishedProduct finishedProduct) {
        finishedProduct.setDeptId(SecurityUtils.getLoginUser().getCompanyId());
        int num = finishedProductService.addFinishedProduct(finishedProduct);
        if (num > 0) {
            return AjaxResult.success("添加成功");
        } else {
            return AjaxResult.error("添加失败");
        }
    }

    /**
     * 查询所有的成品入库记录
     */
    @GetMapping("/list")
    @ResponseBody
    public TableDataInfo selectFinishedProductList(@RequestParam(value = "beginTime", required = false) String beginTime,
                                         @RequestParam(value = "endTime", required = false) String endTime) {
        FinishedProduct finishedProduct = new FinishedProduct();
        finishedProduct.setBeginTime(beginTime);
        finishedProduct.setEndTime(endTime);
        startPage();
        finishedProduct.setDeptId(SecurityUtils.getLoginUser().getUser().getCompanyId());
        List<FinishedProduct> list = finishedProductService.selectFinishedProductList(finishedProduct);
        return getDataTable(list);
    }
    /*
       查询已经做完的成品入库记录
*/
    @GetMapping("/selectEdit")
    public FinishedProduct selectEdit(@RequestParam(value = "finishedProductId", required = false) Long finishedProductId) {
        FinishedProduct finishedProduct = finishedProductService.selectEdit(finishedProductId);
        return finishedProduct;
    }
    /**
     * 删除成品入库记录
     */
    @Log(title = "删除成品入库记录", businessType = BusinessType.DELETE)
    @GetMapping("/remove/{finishedProductId}")
    @ResponseBody
    public AjaxResult remove(@PathVariable("finishedProductId") Long finishedProductId) {
        finishedProductService.remove(finishedProductId);
        return AjaxResult.success("删除成功");
    }

    /*
      编辑成品入库记录
         */
    @PostMapping("/updateFinishedProduct")
    @ResponseBody
    public AjaxResult updateFinishedProduct(@RequestBody FinishedProduct finishedProduct) {
        int num = finishedProductService.updateFinishedProduct(finishedProduct);
        if (num > 0) {
            return AjaxResult.success("修改成功");
        } else {
            return AjaxResult.error("修改失败");
        }
    }


}
